//
//  BwtNativeExpressAdManager.m
//  bwt_ad_flutter
//
//  Created by ciba on 2024/10/11.
//

#import "BwtNativeExpressAdManager.h"
#import "BwtAdLogManager.h"

@implementation BwtNativeExpressAdManager

/// 单例
+ (BwtNativeExpressAdManager *)getBwtNativeExpressAdManager {
    static BwtNativeExpressAdManager *bwtNativeExpressAdManager;
    if(!bwtNativeExpressAdManager) {
        bwtNativeExpressAdManager = [[BwtNativeExpressAdManager alloc] init];
        [bwtNativeExpressAdManager initData];
    }
    return bwtNativeExpressAdManager;
}

/// 初始化
- (void)initData {
    if (!_nativeExpressAdMap) {
        _nativeExpressAdMap = [[NSMutableDictionary alloc] initWithCapacity:0];
    }
}

/// 加载信息流模板广告
- (void)loadNativeExpressAd:(AdParams*)adParams  isRequestIdfa:(bool)isRequestIdfa {
    BwtNativeExpressAd *nativeExpressAd = [BwtNativeExpressAd alloc];
    nativeExpressAd.adParams = adParams;
    nativeExpressAd.isRequestIdfa = isRequestIdfa;
    
    NSString *adId = adParams.adId;
    [_nativeExpressAdMap setObject:nativeExpressAd forKey:adId];
    
    [[BwtAdLogManager getBwtAdLogManager] log:@"开始加载信息流模板广告, adId: %@", adParams.adId];
    [nativeExpressAd loadNativeExpressAd];
}

/// 释放信息流模板广告
- (UIView<CXHAdapterNativeExpressAdViewDelegate> *)getNativeExpressAdView:(NSString*)adId {
    if (adId) {
        BwtNativeExpressAd *nativeExpressAd = _nativeExpressAdMap[adId];
        if (nativeExpressAd) {
            return nativeExpressAd.nativeExpressAdView;
        }
    }
    return nil;
}

/// 释放信息流模板广告
- (void)releaseAd:(NSString*)adId {
    if (adId) {
        BwtNativeExpressAd *nativeExpressAd = _nativeExpressAdMap[adId];
        if (nativeExpressAd) {
            [nativeExpressAd releaseAd];
            [_nativeExpressAdMap removeObjectForKey:adId];
            
            [[BwtAdLogManager getBwtAdLogManager] log:@"信息流模板广告已释放, adId: %@", adId];
        }
    }
}
@end
